package calbs.socket.client;

import calbs.socket.pojo.req.ReqBaseMsg;
import io.netty.buffer.ByteBuf;
import io.netty.handler.timeout.IdleState;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.ReferenceCountUtil;

/**
 * Created by zhangqin on 2017-06-27.
 */
public class NettyClientHandler extends SimpleChannelInboundHandler<ReqBaseMsg> {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            if (e.state() == IdleState.READER_IDLE) {
                ctx.close();
                System.out.println("READER_IDLE 读超时");
            } else if (e.state() == IdleState.WRITER_IDLE) {
                System.out.println("WRITER_IDLE 写超时");
            }
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ReqBaseMsg baseMsg) throws Exception {
        //System.out.println("接收到服务端消息+"+ JSON.toJSONString(baseMsg));
        //channelHandlerContext.writeAndFlush(ModelToString.model2String(baseMsg));
    }
}
